﻿''' <summary>インテリセンス用クラス</summary>
''' <remarks></remarks>
Public Class TreeNodes

#Region "列挙 Enum"
    ''' <summary>ノードのアイテム種別</summary>
    Public Enum NodeTypes As Integer
        [Namespace] = 0
        [Interface]
        [Class]
        [Method]
        [Property]
        [Field]
        [Enum]
        [ValueType]
        [Event]
        [Primitive]
    End Enum
#End Region

#Region "変数 Variables"
    Private _nodes As New List(Of TreeNodes)
#End Region

#Region "プロパティ Property"
    Public Property Name As String = ""
    Public Property AddStrings As String = ""
    Public Property ItemType As NodeTypes
    Public Property SystemType As Type
    Public Property Description As String
    Public Property Parent As TreeNodes

    Private _syncLock As New Object

    Public ReadOnly Property Nodes As List(Of TreeNodes)
        Get
            Return _nodes
        End Get
    End Property
#End Region

#Region "メソッド Method"
    Public Sub AddNode(ByVal target As TreeNodes)
        SyncLock (_syncLock)
            _nodes.Add(target)
        End SyncLock
    End Sub

    Public Function GetFullPath() As String
        Dim result As String = Me.Name
        If Parent IsNot Nothing Then
            Dim parentString = Parent.GetFullPath
            If parentString.Trim <> "" Then result = parentString + "." + result
        End If
        Return result
    End Function

    Public Function SearchNodes(ByVal namePath As String) As TreeNodes
        Return Me.SearchNodesInPrivate(Me, namePath)
    End Function
    Private Function SearchNodesInPrivate(ByVal targetNodes As TreeNodes, ByVal namePath As String) As TreeNodes
        Dim targetPath = namePath.Split(".")
        Dim validPath As Boolean = False
        Dim existsNodes As TreeNodes = Nothing

        Dim validNode = From x In targetNodes.Nodes
                                    Where x.Name.ToLower = targetPath(0).ToLower
                                    Select x

        If (validNode IsNot Nothing) AndAlso (validNode.Count > 0) Then
            existsNodes = validNode.FirstOrDefault
            validPath = True
        End If

        If Not validPath Then Return targetNodes

        Dim nextPath = namePath.Substring(targetPath(0).Length, namePath.Length - targetPath(0).Length)
        If nextPath.StartsWith(".") Then nextPath = nextPath.Substring(1, nextPath.Length - 1)
        If nextPath.Trim = "" Then Return existsNodes
        Return Me.SearchNodesInPrivate(existsNodes, nextPath)
    End Function

#End Region

End Class